/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package org.example.ui.component
{

import org.example.ui.component.supportClasses.LoginComponentState;
import org.example.ui.component.supportClasses.LoginComponentToolKitFactory;
import org.example.ui.model.ILoginComponentModel;
import org.teotigraphix.tka.patterns.component.ToolKitComponent;

import spark.components.Button;
import spark.components.TextInput;
import spark.primitives.supportClasses.TextGraphicElement;

//--------------------------------------
//  Styles
//--------------------------------------

/**
 * The border color of the login component.
 * 
 * <p>Note [StyleBinding] in LoginSkin.</p>
 * 
 * @see org.example.ui.skin.LoginSkin#borderColor
 */
[Style(name="borderColor", type="Color")]

/**
 * The border color of the component when the state is LOGGIN_IN.
 * 
 * <p>Note [StyleBinding] in LoginSkin.</p>
 * 
 * @see org.example.ui.skin.LoginSkin#borderColorLoggingIn
 */
[Style(name="borderColorLoggingIn", type="Color")]

//--------------------------------------
//  Events
//--------------------------------------

/**
 * Dispatched when the login button is pressed.
 */
[Event(name="login", type="org.example.ui.event.LoginEvent")]

//--------------------------------------
//  SkinStates
//--------------------------------------

/**
 * Non-logged in state of the component.
 * 
 * @see org.example.ui.component.supportClasses.LoginComponentState#LOGIN
 */
[SkinState("login")]

/**
 * Current login state of the component.
 * 
 * @see org.example.ui.component.supportClasses.LoginComponentState#LOGGIN_IN
 */
[SkinState("loggingIn")]

/**
 * Successful login state of the component.
 * 
 * @see org.example.ui.component.supportClasses.LoginComponentState#LOGGED_IN
 */
[SkinState("loggedIn")]

//--------------------------------------
//  Class
//--------------------------------------

// How do you make lasagna out of spagetti? Layers...

/**
 * The LoginComponent class provides labels, input text fields, a login button,
 * and messaging for a login form.
 *
 * @author Michael Schmalle
 * @copyright Teoti Graphix, LLC
 */
public class LoginComponent 
	   extends ToolKitComponent
	   implements ILoginComponent
{
	//--------------------------------------------------------------------------
	//
	//  ILoginComponent API :: Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  loggingInMessage
	//----------------------------------
	
	[Bindable]
	
	/**
	 * @copy ILoginComponent#loggingInText
	 */
	public var loggingInText:String;
	
	//----------------------------------
	//  loggedInText
	//----------------------------------
	
	[Bindable]
	
	/**
	 * @copy ILoginComponent#loggedInText
	 */
	public var loggedInText:String;
	
	//----------------------------------
	//  loginMessageText
	//----------------------------------
	
	[Bindable]
	
	/**
	 * @copy ILoginComponent#loginMessageText
	 */
	public var loginMessageText:String;	
	
	//----------------------------------
	//  loginLabel
	//----------------------------------
	
	[Bindable]
	
	/**
	 * @copy ILoginComponent#loginLabel
	 */
	public var loginLabel:String;
	
	//----------------------------------
	//  passwordLabel
	//----------------------------------
	
	[Bindable]
	
	/**
	 * @copy ILoginComponent#passwordLabel
	 */
	public var passwordLabel:String;
	
	//--------------------------------------------------------------------------
	//
	//  Public [SkinPart] :: Variables
	//
	//--------------------------------------------------------------------------
	
	[SkinPart(required="true")]
	
	/**
	 * The login button.
	 */
	public var loginButton:Button;
	
	[SkinPart(required="true")]
	
	/**
	 * The username input.
	 */
	public var loginInput:TextInput;
	
	[SkinPart(required="true")]
	
	/**
	 * The password input.
	 */
	public var passwordInput:TextInput;
	
	[SkinPart(required="false")]
	
	/**
	 * The username input label.
	 */
	public var loginInputLabel:TextGraphicElement;
	
	[SkinPart(required="false")]
	
	/**
	 * The password input label.
	 */
	public var passwordInputLabel:TextGraphicElement;
	
	[SkinPart(required="false")]
	
	/**
	 * The login stats message label.
	 */
	public var loginMessage:TextGraphicElement;
	
	//--------------------------------------------------------------------------
	//
	//  Constructor
	//
	//--------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function LoginComponent()
	{
		super();
		
//		setStyle("skinClass", LoginSkin);
		setStyle("toolKitFactoryClass", LoginComponentToolKitFactory);
	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Public :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
//	override public function initializeToolKit():void
//	{
//		super.initializeToolKit();
//		
//		coreModel = new LoginComponentCoreModel();
//		coreBehavior = new LoginComponentCoreBehavior();
//	}
	
	//--------------------------------------------------------------------------
	//
	//  Overridden Protected :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	override protected function getCurrentSkinState():String
	{
		if (coreModel is ILoginComponentModel)
			return ILoginComponentModel(coreModel).getSkinState();
		return LoginComponentState.LOGIN;
	}
	
	//--------------------------------------------------------------------------
	//
	//  ILoginComponent API :: Methods
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @copy ILoginComponent#loginSucceeded()
	 */
	public function loginSucceeded():void 
	{
		if (coreModel is ILoginComponentModel)
			ILoginComponentModel(coreModel).loginSucceeded();
	}
	
	/**
	 * @copy ILoginComponent#loginFailed()
	 */
	public function loginFailed():void 
	{
		if (coreModel is ILoginComponentModel)
			ILoginComponentModel(coreModel).loginFailed();
	}
}
}